package com.teach.service.impl;

import com.teach.dao.AdminInfoDAO;
import com.teach.dao.impl.AdminInfoDAOImpl;
import com.teach.dto.AuthDto;
import com.teach.entity.AdminInfo;
import com.teach.service.AdminService;
import com.teach.util.MD5;

/**
 * <pre>
 * -
 * Author : J.L.Zhou
 * E-Mail : 2233875735@qq.com
 * Tel : 151 1104 7708
 * Date : 2022-05-11 9:49
 * Version : 1.0
 * Copyright 2022 jlzhou.top Inc. All rights reserved.
 * Warning: this content is only for internal circulation of the company.
 *          It is forbidden to divulge it or use it for other commercial purposes.
 * </pre>
 **/
public class AdminServiceImpl implements AdminService {

  private AdminInfoDAO dao = new AdminInfoDAOImpl();
  @Override
  public AuthDto login(String name, String pwd) throws Exception {
    //参数校验
    if(name==null||pwd==null||"".equals(name)||"".equals(pwd)){
      throw new Exception("用户名和密码不能为空");
    }
    //根据用户获取用户对象
    AdminInfo adminInfo = null;
    try{
      adminInfo = dao.selectByName(name);
    }catch (Exception ex){
      throw new Exception("用户不存在",ex);
    }
    //比较密码的正确性
    if(!MD5.encode(pwd).equals(adminInfo.getAdminPwd())){
      throw new Exception("密码错误");
    }
    AuthDto auth  = new AuthDto();
    auth.setAdminId(adminInfo.getAdminId());
    auth.setAdminName(adminInfo.getAdminName());
    return auth;
  }
}
